﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    public class ExamResultsLogDAL
    {
        public static DataSet GetLogs(int ExamResultID)
        {
            string sql = @"SELECT DISTINCT
                            a.ExamResultsLogID,
                            CONCAT(a.Score) AS StepScore,
                            a.OperationTime,
                            a.Parameter,
                            a.TaskID,
                            CASE
                                WHEN a.TaskID = 0 THEN '无效'
                                ELSE CASE
                                    WHEN a.iscorrect = 0 THEN '错误'
                                    ELSE '正确'
                                END
                            END IsCorrect,
                            LEFT(b.`Describe`, 3) AS `QuestionDescribe`,
                            CASE
                                WHEN
                                    a.TaskID = 0
                                THEN
                                    CASE
                                        WHEN a.taskitemid > 0 THEN '无效任务'
                                        ELSE '无效操作'
                                    END
                                ELSE c.Tooltips
                            END Tooltips,
                            c.OperationTooltips,
                            CASE
                                WHEN
                                    a.TaskID = 0
                                THEN
                                    CASE
                                        WHEN
                                            a.taskitemid > 0
                                        THEN
                                            (SELECT 
                                                    `Describe`
                                                FROM
                                                    taskitem
                                                WHERE
                                                    TaskID = a.TaskItemID)
                                        ELSE NULL
                                    END
                                ELSE d.`Describe`
                            END TaskDescribe,
                            f.`Describe` AS StepDescribe,
                            a.TaskItemID,
                            CONCAT(c.TargetOperationScore) as Score
                        FROM
                            examresultslog a
                                LEFT JOIN
                            testquestion b ON a.QuestionID = b.TestQuestionID
                                LEFT JOIN
                            testquestiontask c ON a.TaskID = c.TestQuestionTaskID
                                LEFT JOIN
                            taskitem d ON c.TaskID = d.TaskID
                                LEFT JOIN
                            stepitem f ON a.StepID = f.StepID
                                LEFT JOIN
                            testquestiontaskstep g ON c.TestQuestionTaskID = g.TestQuestionTaskID
                                AND g.StepID = a.StepID
                        WHERE
                            a.ExamResultID = " + ExamResultID + " order by OperationTime";

            return DbHelper.ExecuteDataset(sql);
        }

        public static void AddList(List<ExamResultsLogModel> logs)
        {
            foreach (var log in logs)
            {
                string sql = @"INSERT INTO `examresultslog`
                            (`OperationLogID`,
                            `ExamQuestionID`,
                            `ExamTaskID`,
                            `ExamSetpID`,
                            `LogType`,
                            `ExamresultID`)
                            VALUES
                            (
                            @OperationLogID,
                            @ExamQuestionID,
                            @ExamTaskID,
                            @ExamSetpID,
                            @LogType,
                            @ExamresultID); ";
                MySqlParameter[] parms = {
                    new MySqlParameter("@OperationLogID", log.OperationLogID),
                    new MySqlParameter("@ExamQuestionID", log.ExamQuestionID),
                    new MySqlParameter("@ExamTaskID", log.ExamTaskID),
                    new MySqlParameter("@ExamSetpID", log.ExamSetpID),
                    new MySqlParameter("@LogType", log.LogType),
                    new MySqlParameter("@ExamresultID", log.ExamresultID)
                };

                DbHelper.ExecuteNonQuery(sql, parms);
            }
        }

        public static List<ExamResultsLogModel> GetExamResultsList(int ExamResultsTestQuestionID)
        {
            //string sql = @"SELECT 
            //            a.LogType,
            //            a.`ExamSetpID`,
            //            b.OperID,
            //            e.`Describe` AS operDescribe,
            //            b.DeviceTypeID,
            //            d.TypeName,
            //            b.DeviceID,
            //            b.parameters,
            //            c.StepDescribe
            //        FROM
            //            examresultslog a
            //                INNER JOIN
            //            operationlog b ON a.`OperationLogID` = b.id
            //                INNER JOIN
            //            examresultstestquestiontaskstep c ON a.`ExamSetpID` = c.id
            //                INNER JOIN
            //            devicetype d ON b.DeviceTypeID = d.id
            //                INNER JOIN
            //            stepitem e ON e.StepID = b.operid
            //       where a.ExamQuestionID = " + ExamResultsTestQuestionID;
            //DataSet ds = DbHelper.ExecuteDataset(sql);

            string sql = @"SELECT 
                        a.LogType,
                        a.`ExamSetpID`,
                        b.OperID,
                        e.`Describe` AS operDescribe,
                        b.DeviceTypeID,
                        d.TypeName,
                        f.DevName,
                        b.DeviceID,
                        b.parameters,
                        c.StepDescribe
                    FROM
                        examresultslog a
                            INNER JOIN
                        operationlog b ON a.`OperationLogID` = b.id
                            INNER JOIN
                        examresultstestquestiontaskstep c ON a.`ExamSetpID` = c.id
                            INNER JOIN
                        devicetype d ON b.DeviceTypeID = d.id
                            INNER JOIN
                        device f ON f.DeviceID = b.DeviceID
                            AND f.DevType = b.DeviceTypeID
                            INNER JOIN
                        stepitem e ON e.StepID = b.operid
                   where a.ExamQuestionID = " + ExamResultsTestQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<ExamResultsLogModel> list = new List<ExamResultsLogModel>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetTrainLateModel(dt.Rows[i]));
                }
            }
            return list;
        }
        //获取乘客数值
        public static List<PassenergerPointModel> GetPassenger(int ExamResultsTestQuestionID)
        {

            String sql = "SELECT * FROM passenergerpoint where ExamResultId=(SELECT ExamResultsID FROM examresultstestquestion where ID="+ ExamResultsTestQuestionID+");" ;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<PassenergerPointModel> list = new List<PassenergerPointModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetPassengerModel(dt.Rows[i]));
                }

            }
            return list;
        }

        public static List<String> GetLateTrain(int ExamResultsTestQuestionID)
        {

            string sql = @"SELECT StopsRunningTrain FROM
                        `examresultstestquestion` 
                   where Id = " + ExamResultsTestQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<String> list = new List<String>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    String train =dt.Rows[0]["StopsRunningTrain"].ToString();
                    
                    if (!train.Contains(","))
                    {
                        list.Add(train);
                    }
                    else
                    {

                        for(int ii = 0; ii < train.Split(',').Count(); ii++)
                        {
                            list.Add(train.Split(',')[ii]);
                        }
                    }
                    

                    
                }
            }
            return list;
        }



        internal static ExamResultsLogModel GetTrainLateModel(DataRow dr)
        {
            ExamResultsLogModel info = new ExamResultsLogModel();

            info.LogType = Convert.ToInt32(dr["LogType"]);
            info.OperDescribe = dr["operDescribe"].ToString();
            info.DeviceTypeName = dr["TypeName"].ToString();
            info.DeviceName = dr["DevName"].ToString();
            info.StepDescribe = dr["StepDescribe"].ToString();
            info.ExamSetpID= Convert.ToInt32(dr["ExamSetpID"]);
            info.Parameters = dr["Parameters"].ToString();
            return info;
        }
        internal static PassenergerPointModel GetPassengerModel(DataRow dr)
        {
            PassenergerPointModel info = new PassenergerPointModel();

            info.ExamResultId = Convert.ToInt32(dr["ExamResultId"]);
            info.PassengerNum = dr["PassengerNum"].ToString();
            info.PlatformName = dr["PlatformName"].ToString();
            info.DeleteScore = dr["DeleteScore"].ToString();
            return info;
        }
    }
}
